package ac.github.os.internal.core.condition.impl.block

import ac.github.os.internal.core.condition.Condition
import ac.github.os.internal.core.config.StringConfig
import ac.github.os.internal.core.config.get
import ac.github.os.internal.core.holder.BlockCompatible
import ac.github.os.internal.session.Session
import org.bukkit.Location
import taboolib.common.platform.Awake

abstract class BlockCondition(val name : String) : Condition {

    override val namespace: String
        get() = "block:$name"

    override fun doScreen(session: Session, config: StringConfig): Boolean {
        val holder = session.holder
        if (holder is BlockCompatible && holder.getBlock() != null) {
            return inlineScreen(session,config,holder)
        }
        return false
    }

    abstract fun inlineScreen(session: Session,config: StringConfig,blockCompatible: BlockCompatible) : Boolean

}

@Awake
class BlockLocationCondition : BlockCondition("location") {
    override fun inlineScreen(session: Session, config: StringConfig, blockCompatible: BlockCompatible): Boolean {
        val location = blockCompatible.getBlockLocation()
        val get = config.get<Location>("val", single = true)!!
        return location.blockX == get.blockX && location.blockY == get.blockY && location.blockZ == get.blockZ
    }
}

@Awake
class BlockTypeCondition : BlockCondition("type") {
    override fun inlineScreen(session: Session, config: StringConfig, blockCompatible: BlockCompatible): Boolean {
        val blockType = blockCompatible.getBlockType()
        return config.get("val","AIR",single = true)!! == blockType.name
    }
}